package com.tpc.control.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;


public class Formatting {
	public static String DateToString(Date var) {
		if(var != null){
			Locale aLocale = new Locale("th", "US");
			DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy", aLocale);
			return formatter.format(var);
		}else{
			return "";
		}
	}
	
	public static String quantityToString(Double var) {
		DecimalFormat df1 = new DecimalFormat("#,##0.000");
		return df1.format(var.doubleValue());
	}
	
	public static String leadingChar(String var, char lead, int length) {
		while (var.length() < length) {
			var = lead + var;
		}
		return var;
	}
	
	public static String checkStringNull(String value) {
		if (value == null) {
			return "";
		} else {
			return value;
		}
	}
	
	public static String[] generateFromToText(Object id1, Object id2, int len) {
		String[] value = new String[2];
		
		value[0] =(String)id1;
		value[1] = (String)id2;
		
		if (!value[0].equals("") && value[1].equals("")) {
			value[1] = value[0];
		} else if (value[0].equals("") && !value[1].equals("")) {
			value[0] = value[1];
		} else if (value[0].equals("") && value[1].equals("")) {
			value[0] = Formatting.leadingChar(value[0], '0', len);
			value[1] = Formatting.leadingChar(value[1], 'z', len);
		}
		return value;
	}
	
	public static Date[] generateFromToDate(Object id1, Object id2) {
		Date[] value = new Date[2];
		
		value[0] =(Date)id1;
		value[1] = (Date)id2;
		
		if (value[0]!=null && value[1]==null) {
			value[1] = value[0];
		} else if (value[0]==null && value[1]!=null) {
			value[0] = value[1];
		} else if (value[0]==null && value[1]==null) {
			Calendar aCalendar = Calendar.getInstance();
			aCalendar.set(Calendar.DAY_OF_MONTH, 01);
			aCalendar.set(Calendar.MONTH, Calendar.JANUARY);
			aCalendar.set(Calendar.YEAR, 1970);
			value[0] = aCalendar.getTime();
			
			aCalendar = Calendar.getInstance();
			aCalendar.set(Calendar.DAY_OF_MONTH, 31);
			aCalendar.set(Calendar.MONTH, Calendar.DECEMBER);
			aCalendar.set(Calendar.YEAR, 9999);
			value[1] = aCalendar.getTime();
		}
		
		return value;
	}
	
	public static String generateCustomerNo(String value) {
		try {
			int customerNo = Integer.valueOf(value);
			value = Formatting.leadingChar(value, '0', 10);
		} catch (Exception e) {
			System.out.println("cannot convert customer: "+value+" to integer");
		}
		return value;
	}
	
	public static String DateToFormat(Date var, String format) {
		if (var != null) {
			DateFormat formatter = new SimpleDateFormat(format, new Locale("th", "US"));
			String s = formatter.format(var);
			return s;
		} else {
			return "";
		}
	}
	
	public static ResourceBundle getMessageBundle() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ResourceBundle rb = ResourceBundle.getBundle(
				"com.tpc.control.message.ResourceBundle", facesContext
						.getViewRoot().getLocale());
		return rb;
	}
	
	public static String byteToString(byte[] var) {
		StringBuffer hexString = new StringBuffer();
		for(int i=0;i<var.length;i++) {
			String text = Integer.toHexString(0xFF & var[i]);
			if (text.length() < 2) {
				text = "0"+text;
			}
			hexString.append(text);
		}
		return hexString.toString().toUpperCase();
	}
}
